package com.scoy.honeymei.http;

import android.app.Activity;
import android.app.ProgressDialog;
import android.view.Window;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.scoy.honeymei.bean.BaseBean;
import com.scoy.honeymei.bean.LoginBean;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.LogUtil;
import com.scoy.honeymei.utils.MyUtil;

/**
 * ClassName:      com.shanchuang.propertycustomer.http
 * Description:
 * Author:         GaoZhan & Administrator
 * UpdateDate:     2019/10/11/0011$ 下午 14:02$
 * UpdateRemark:   更新内容
 */
public class HpGo {

    private Activity activity;
    private String mess = "请求网络中...";
    private static HpGo instance;

    private HpGo() {
    }

  /*  private HpGo(Activity activity) {
        this.activity = activity;
    }*/

    public synchronized static HpGo newInstance() {
        if (instance == null) {
            instance = new HpGo();
        }
        return instance;
    }

    private ProgressDialog dialog;

    private void showLoading(Activity activity) {
        if (dialog != null && dialog.isShowing()) return;
        dialog = new ProgressDialog(activity);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setMessage(mess);
        dialog.show();
    }

    private void dismissLoading() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    public void HttpGo(Activity activity, String link, HttpParams params, HpCallback callback) {
        showLoading(activity);
        OkGo.<String>post(link).params(params).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                dismissLoading();
                String body = response.body();
                LogUtil.e(body);
                BaseBean baseBean = new Gson().fromJson(body, BaseBean.class);
                String code = baseBean.getCode();
                String msg = baseBean.getMsg();
                Object data = baseBean.getData();
                String sdata = "";
                if (data instanceof String) {
                    sdata = (String) data;
                } else if (data instanceof Boolean) {
                    sdata = "" + data;
                } else {
                    sdata = new Gson().toJson(data);
                }
                if ("200".equals(code)) {
                    callback.onSuccess(sdata);
                } else {
                    callback.onFailed(code, msg, sdata);
                }

            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                dismissLoading();
                callback.onError(response.code(), response.message());
            }
        });
    }


    public HpGo setMess(String mess) {
        this.mess = mess;
        return this;
    }
}
